package com.h3bpm.kumhosunny.sample.controller.utils;
/**
 * @Auther: Administrator
 * @Date: 2019/11/28 17:46
 * @Description:
 */

import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataRow;
import data.DataTable;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: 样品单号生成工具类
 * @Description: 描述
 *
 * @author WJQ
 * @version V1.0
 * @Date
 */
public class SampleNoUtil {

    /**
     *
     //根据成员所在部门生成对应的样品编码
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/11/27 11:22
     */

    public static Map<String,String> sampleNember(String address){
        if(address==null||address.isEmpty()){
            return  null;
        }
        Map<String, String> sampleMap = newSampleNo(getApplicationNoAndItemNo(address),address);
        //判断项目是否存在
        return sampleMap;
    }

    /**
     *
     //数据库提取最新的一条记录
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/11/28 18:22
     */
    public static Map<String,String> getApplicationNoAndItemNo(String addr){
        try {
            Map<String, String> sampleMap =  Collections.synchronizedMap(new HashMap<String, String>(16));
           String head ="";
            if("佛山".indexOf(addr)!=-1){
                head="GD";
            } else{
                head="SH";
            }
            String sql = "select itemNo,applicationNo from i_sampleapplication where itemNo like '"+head+"%'  ORDER BY  itemNo desc LIMIT 1";
            DataTable dataTable = SqlUtil.doQuery(sql);
            List<DataRow> entityRows = dataTable.getEntityRows();
            String applicationNo = null;
            String itemNo=null;
            if (entityRows.isEmpty()) {
            } else {
                DataRow dataRow = entityRows.get(0);
                applicationNo = dataRow.getString(0);
                itemNo = dataRow.getString(1);
            }
            //判断项目是否存
            if(applicationNo==null||applicationNo.isEmpty()||itemNo==null||itemNo.isEmpty()){
                return null;
            }else{
                sampleMap.put("applicationNo",applicationNo);
                sampleMap.put("itemNo",itemNo);
            }
            return sampleMap;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     //根据样品名称生成对应的样品编码
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/11/28 19:22
     */
    public static Map<String,String> newSampleNo(Map<String, String> sample ,String address){
        //当前时间
        Map<String, String> sampleMap =  Collections.synchronizedMap(new HashMap<String, String>(16));
        Integer mon = DateUtil.getMonth();

        Integer year =  DateUtil.getYear();
        String month = mon.toString();
        if(mon>=10) {
            month = mon.toString();
        }else{
            month = "0"+mon.toString();
        }
        String  years = year.toString();
        String strYear = years.substring(years.length() -2,years.length());   //截取 19
        String newApplicationNo="";
        String newItemNo="";
        String head="";
        if("佛山工厂营业部".equals(address)){
            head="GD";
        } else{
            head="SH";
        }
        if(sample==null||sample.isEmpty()){
            newItemNo= head+strYear+month+"0001";
            newApplicationNo=newItemNo+"-01";
            sampleMap.put("applicationNo",newApplicationNo);
            sampleMap.put("itemNo",newItemNo);
            return  sampleMap;
        }
        String applicationNo = sample.get("applicationNo");//S19110001-01
        System.out.println(applicationNo);
        //截取以存在的的月份和年份编号,尾号 SH19110001-01
        String oldYear=applicationNo.substring(2,4);
        String oldMonth=applicationNo.substring(4,6);
        String oldNo=applicationNo.substring(6,10);
        if(!strYear.equals(oldYear)){
            newItemNo= head+strYear+month+"0001";
            newApplicationNo=newItemNo+"-01";
            sampleMap.put("applicationNo",newApplicationNo);
            sampleMap.put("itemNo",newItemNo);
            return  sampleMap;
        }if (!oldMonth.equals(month)){
            newItemNo= head+strYear+month+"0001";
            newApplicationNo=newItemNo+"-01";
            sampleMap.put("applicationNo",newApplicationNo);
            sampleMap.put("itemNo",newItemNo);
            return  sampleMap;
        }
        Integer nember=Integer.valueOf(oldNo);
        if(nember>0) {
            String no=NemberUtil.nemberStr4(nember+1);
            newItemNo= head+strYear+month+no;
            newApplicationNo=newItemNo+"-01";
            sampleMap.put("applicationNo",newApplicationNo);
            sampleMap.put("itemNo",newItemNo);
            return  sampleMap;
        }
        return sampleMap;
    }
    /**
     *
     //数据库提取的一条记录 根据applicationNo itemNo
     *
     * @param:
     * @return:
     * @auther: Administrator
     * @date: 2019/11/28 18:22
     */
    public static String  checkApplicationNoAndItemNo(String itemNo,String ObjectId){
        String applicationNo = null;
        try {
            String sql = "select applicationNo,ObjectID from i_sampleapplication where itemNo='"+itemNo+"' and applicationNo is not null   and applicationNo !=''  ORDER BY createdTime desc LIMIT 1";
            DataTable dataTable = SqlUtil.doQuery(sql);
            List<DataRow> entityRows = dataTable.getEntityRows();

            if (entityRows.isEmpty()) {
            } else {
                DataRow dataRow = entityRows.get(0);
            if(ObjectId!=null) {
               String no = dataRow.getString(0).substring( dataRow.getString(0).length()-2, dataRow.getString(0).length());
               Integer intNo =  Integer.valueOf(no);
                if(intNo ==1) {
                    if (!ObjectId.equals(dataRow.getString(1))) {
                        applicationNo = itemNo+"-"+String.format("%02d", intNo+1);
                    }else {
                        applicationNo = itemNo+"-01";
                    }
                }else if (intNo >1){
                        if (ObjectId.equals(dataRow.getString(1))) {
                            applicationNo = dataRow.getString(0);
                        }else {
                            applicationNo = itemNo+"-"+String.format("%02d", intNo+1);
                        }
                    }
               }else{
                 applicationNo = dataRow.getString(0);
               }
            }
            //判断项目是否存
            if(applicationNo==null||applicationNo.isEmpty()) {
                return null;
            }else{
                return applicationNo;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return applicationNo;
    }
    /**
            *
            //根据组织ou 获取name
            *
            * @param:
            * @return:
            * @auther: Administrator
     * @date: 2019/11/28 18:22
            */
    public static String  getOuName(String ou){
        String ouName="";
        if(ou==null||ou.isEmpty()){}
        try {
            String sql = "SELECT `name` FROM ot_organizationunit WHERE ObjectID='"+ou+"'";
            DataTable dataTable = SqlUtil.doQuery(sql);
            List<DataRow> entityRows = dataTable.getEntityRows();
            if (entityRows.isEmpty()) {
            } else {
                DataRow dataRow = entityRows.get(0);
                ouName = dataRow.getString(0);
            }
            //判断项目是否存
            if(ouName==null||ouName.isEmpty()){
                return null;
            }else{
                return ouName;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return ouName;
    }
}

